const Config = require('common/core/config.js');
const Router = require('common/core/router.js');
const SevMode = Config.SevMode;
App({
  SevMode,
  Config,
  Router,
  globalData: {
    openid: '',
    userInfo: null,
    //非云开发数据
    userid: ''
  },
  //先序执行的函数
  onLaunch: function () {
    this.InitCloud();
    //console.log(this.SevMode)

  },
  InitCloud() {
    var that = this;
    //获取缓存信息
    this.globalData.userInfo = wx.getStorageSync('wxuserinfo') || null;
    if (SevMode == 'Cloud') {
      let CloudID = Config.CloudID;
      if (!wx.cloud) {
        console.log('--不支持云开发--')
      } else {
        wx.cloud.init({
          env: CloudID,

          traceUser: Config.TraceUser
        })
        // this.InitCollections();
        //获取OPENID信息
        Router.GetOpenData().then(res => {
          that.globalData.openid = res;
        })
        //初始化创建数据集
      }
    } else {
      //非云开发置换openid,建议这个步骤还是通过云开发来完成
      Router.GetOpenData().then(res => {
        let openid = res;
        that.globalData.openid = res;
        wx.request({
          url: Config.SevDomain + 'auth',
          data: {
            source: 'wx',
            openid: res
          },
          success(res) {
            console.log(res)
            if (res.data.code == 0) {
              let userid = res.data.data;
              console.log('uimsid 是:' + userid)
              that.globalData.userid = userid;
            } else {
              wx.showToast({
                title: '获取uimsid 失败',
              })
            }
          },
          fail(res) {
            wx.showToast({
              title: '获取uimsid 失败',
            })
          }
        })
      })

    }
  },
  InitCollections() {
    //初始化集合
    const db = wx.cloud.database();
    console.log('--初始化集合--')
    //数据库中已经存在集合，可以注释该句
    wx.cloud.callFunction({
      name: 'mapi',
      data: {
        action: 'InitCollection'
      }
    }).then(res => {
      //console.log(res)
    })
  },


})